package com.liuweijiaoyu.demo.util;

import android.content.Context;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class QuestionTemplate {

    private static String choiceTemplate;
    private static String wordQuestionTemplate;
    private static QuestionTemplate instance;

    private QuestionTemplate(){}

    public static QuestionTemplate getIns() {
        if(instance==null) {
            instance = new QuestionTemplate();
        }
        return instance;
    }

    private String getChoiceTemplate(Context context) throws IOException {

        if(choiceTemplate == null) {
            choiceTemplate = readFile(context, "choice.html");
        }
        return choiceTemplate;
    }

    private String getWordQuestionTemplate(Context context) throws IOException {
        if(wordQuestionTemplate == null) {
            wordQuestionTemplate = readFile(context, "word-question.html");
        }
        return wordQuestionTemplate;
    }

    private String readFile(Context context, String path) throws IOException {
        InputStream is = context.getAssets().open(path);
        return readFile2String(is, "UTF-8");
    }

    public static String readFile2String(InputStream is, String charset) {

        ByteArrayOutputStream os = null;
        try {
            os = new ByteArrayOutputStream();
            copyStream(is, os);
            return new String(os.toByteArray(), charset);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "<html><head><meta charset=\"utf-8\"></head><body>题目模板解析失败</body></html>";
    }

    private String parseChoice(OnlineQuestion.QuestionItem qt, String answerMode, String isShowAnswerSheet, String currentPage, String totalPage, String template) {

        String singleMode = "YES";
        if(qt.getQuestionType()==2) {
            singleMode = "NO";
        }
        template = template.replace("${singleMode}", singleMode).replace("${answerMode}", answerMode).replace("${questionType}", qt.getQuestionTypeName()).replace("${trunkContent}", qt.getContent()).replace("${currentPage}", currentPage).replace("${totalPage}", totalPage);

        if("YES".equals(answerMode)) {
            template = template.replace("${analyzeContent}", qt.getAnswerExplain());
        }
        if("NO".equals(isShowAnswerSheet)) {
            template = template.replace("${answersheetDisplay}", "hide");
        }

        for (OnlineQuestion.QuestionSubItem item: qt.getQuestionSubItemList()) {
            template = template.replace("${"+item.getItemCode()+"}", item.getQuestionItem());
        }

        return template;
    }

    private String parseWordQuestion(OnlineQuestion.QuestionItem qt, String answerMode, String isShowAnswerSheet, String currentPage, String totalPage, String template) {

        template = template.replace("${answerMode}", answerMode).replace("${questionType}", qt.getQuestionTypeName()).replace("${trunkContent}", qt.getContent()).replace("${currentPage}", currentPage).replace("${totalPage}", totalPage);

        if("YES".equals(answerMode)) {
            template = template.replace("${analyzeContent}", qt.getAnswerExplain());
        }
        if("NO".equals(isShowAnswerSheet)) {
            template = template.replace("${answersheetDisplay}", "hide");
        }

        return template;
    }

    public String getTemplate(Context context, OnlineQuestion.QuestionItem qt, String answerMode, String isShowAnswerSheet, String currentPage, String totalPage) throws IOException {

        if(isChoice(qt)) {
            return parseChoice(qt, answerMode, isShowAnswerSheet, currentPage, totalPage ,getChoiceTemplate(context));
        }else {
            return parseWordQuestion(qt, answerMode, isShowAnswerSheet, currentPage, totalPage, getWordQuestionTemplate(context));
        }
    }


    /**
     * 判断当前题型是否是选择题，单选或多选
     *
     * @return true 选择题，false 解答题
     */
    public static boolean isChoice(OnlineQuestion.QuestionItem qt) {
        if (qt.getQuestionType() == 1 || qt.getQuestionType() == 2) {
            return true;
        }
        return false;
    }


    /*
	 * 流拷贝
	 */
    public static void copyStream(InputStream is, OutputStream os)
            throws IOException {
        byte buffer[] = new byte[1024];
        int len = -1;
        while ((len = is.read(buffer, 0, 1024)) != -1) {
            os.write(buffer, 0, len);
        }
    }
}
